package org.edu.controller;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.ListDataModel;

import org.edu.model.Lesson;
import org.edu.service.LessonService;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.SelectableDataModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@SessionScoped
@Controller
public class LessonController extends ListDataModel<Lesson> implements
		Serializable, SelectableDataModel<Lesson> {

	private static final long serialVersionUID = 1L;
	@Autowired
	private transient LessonService lessonService;
	private Lesson lesson;
	private List<Lesson> lessonList;
	private List<Lesson> filterLesson;

	public LessonController() {
		lesson = new Lesson();
	}

	@PostConstruct
	public void init() {
		lessonList = lessonService.lessons();
	}

	public void preparedAddLesson() {
		lesson = new Lesson();
	}

	public String save() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		try {
			lessonService.save(lesson);
			facesContext.addMessage(null , new FacesMessage("Succesful" ,"Save Lesson is Succesful"));
			init();
			lesson = new Lesson();
		} catch (Exception e) {
			facesContext.addMessage(null, new FacesMessage("Error!" ,"Not Succesful"));
			e.printStackTrace();
		}
		return null;
	}
	
	public void preparedUpdateLesson(Long lesson_id) throws Exception{
			lesson = lessonService.findById(lesson_id);
	}
	
	public String update() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		try {
			lessonService.update(lesson);
			facesContext.addMessage(null , new FacesMessage("Succesful" ,"Update Lesson is Succesful"));
			init();
			lesson = new Lesson();
		} catch (Exception e) {
			facesContext.addMessage(null, new FacesMessage("Error!" ,"Not Succesful"));
			e.printStackTrace();
		}
		return null;
	}
	
	public void preparedDeleteLesson(Long lesson_id) throws Exception{
		lesson = lessonService.findById(lesson_id);
	}
	
	public String remove() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		try {
			lessonService.remove(lesson);
			facesContext.addMessage(null , new FacesMessage("Delete Lesson is Succesful" , lesson.getLesson_name()));
			init();
			lesson = new Lesson();
		} catch (Exception e) {
			facesContext.addMessage(null, new FacesMessage("Error!" ,"Not Succesful"));
			e.printStackTrace();
		}
		return null;
	}
	
	public void onRowSelect(SelectEvent event) {  
        FacesMessage msg = new FacesMessage("Lesson Selected", ((Lesson) event.getObject()).getLesson_code());  
        FacesContext.getCurrentInstance().addMessage(null, msg);  
	}

	@Override
	public Object getRowKey(Lesson object) {
		return lesson.getLesson_code();
	}
	
	@Override
	public Lesson getRowData(String rowKey) {
		for(Lesson lesson : lessonList){
			if(lesson.getLesson_code().equals(rowKey))
				return lesson;
		}
		return null;
	}

	public Lesson getLesson() {
		if (lesson == null) {
			lesson = new Lesson();
		}
		return lesson;
	}

	public void setLesson(Lesson lesson) {
		this.lesson = lesson;
	}

	public List<Lesson> getLessonList() {
		return lessonList;
	}

	public List<Lesson> getFilterLesson() {
		return filterLesson;
	}

	public void setFilterLesson(List<Lesson> filterLesson) {
		this.filterLesson = filterLesson;
	}

}
